import {ref} from "vue";
import {assign} from "min-dash";
import {BpmnNodeType} from "/@/views/flow/design/component/property-panel/ts/BpmnModel";
import {BpmnNode} from "/@/views/flow/design/component/property-panel/ts/node/BpmnNode";
import {UserTaskNode} from "/@/views/flow/design/component/property-panel/ts/node/impl/UserTaskNode";
import {SequenceFlowNode} from "/@/views/flow/design/component/property-panel/ts/node/impl/SequenceFlowNode";
import {ProcessNode} from "/@/views/flow/design/component/property-panel/ts/node/impl/ProcessNode";
import {StartEventNode} from "/@/views/flow/design/component/property-panel/ts/node/impl/StartEventNode";
import {EndEventNode} from "/@/views/flow/design/component/property-panel/ts/node/impl/EndEventNode";
const bpmnViewer = ref()
const propertyPanelRef = ref()
const eventMap = new Map<string,BpmnNode>();
eventMap.set(BpmnNodeType.Process,new ProcessNode())
eventMap.set(BpmnNodeType.StartEvent,new StartEventNode())
eventMap.set(BpmnNodeType.EndEvent,new EndEventNode())
eventMap.set(BpmnNodeType.UserTask,new UserTaskNode())
eventMap.set(BpmnNodeType.SequenceFlow,new SequenceFlowNode())
const businessObject = ref({
    name: ''
})

export function bindEvent(bpmnViewerValue: any,propertyPanelRefValue: any){
    propertyPanelRef.value = propertyPanelRefValue
    bpmnViewer.value = bpmnViewerValue
    const eventBus = bpmnViewer.value.get('eventBus')
    eventBus.on('element.click', (e:any)=>{
        nodeClick(e)
    })
    eventBus.on('element.changed', (e:any)=>{
        nodeChanged(e)
    })
    eventBus.on('create.end',(e: any,e1)=>{
        nodeCreate(e)
    })
}

/**
 * 节点单机事件
 */
function nodeClick(e:any) {
    const bpmnNode = eventMap.get(e.element.type)
    if(bpmnNode){
        bpmnNode.setPropertyPanelRef(propertyPanelRef.value)
        bpmnNode.nodeClick(bpmnViewer.value,e)
    }else{
        console.warn("此节点事件暂不存在",e)
    }

}

/**
 * 节点变更
 */
function nodeChanged(e:any) {
    const bpmnNode = eventMap.get(e.element.type)
    if(bpmnNode){
        bpmnNode.nodeChange(bpmnViewer.value,e)
    }else{
        console.warn("此节点事件暂不存在",e)
    }
}

/**
 * 节点创建
 */
function nodeCreate(e) {
    const bpmnNode = eventMap.get(e.shape.type)
    if(bpmnNode){
        bpmnNode.nodeCreate(bpmnViewer.value,e)
    }else{
        console.warn("此节点事件暂不存在",e)
    }
}



